﻿using Dyao.Book.Application.Abstractions;
using Dyao.Book.Application.Extensions;
using Dyao.Book.Contract;
using Dyao.Book.Repository.Abstractions;
using Foundation.Caching;

namespace Dyao.Book.Application.Manager
{
    public class BookSourceWarehouseManager : BaseManager, IBookSourceWarehouseManager
    {
        /// <summary>
        /// 源仓库仓储
        /// </summary>
        private readonly IBookSourceWarehouseRepository _sourceWarehouseRepository;

        /// <summary>
        /// 分布式缓存
        /// </summary>
        private readonly IDistributedMultiLevelCache _distributedMultiLevelCache;

        public BookSourceWarehouseManager(IBookSourceWarehouseRepository sourceWarehouseRepository,
            IDistributedMultiLevelCache distributedMultiLevelCache)
        {
            _sourceWarehouseRepository = sourceWarehouseRepository;
            _distributedMultiLevelCache = distributedMultiLevelCache;
        }

        /// <inheritdoc cref="IBookSourceWarehouseManager.GetSourceWarehousesForCacheAsync"/>
        public async Task<List<BookSourceWarehouseDto>> GetSourceWarehousesForCacheAsync(CancellationToken cancellationToken)
        {
            var result = await _distributedMultiLevelCache.GetOrAddAsync(CacheConstants.SourceWarehouseCache, async () =>
            {
                var sources = await _sourceWarehouseRepository.GetAllListAsync(cancellationToken);
                return sources.ToList();
            }, TimeSpan.FromMinutes(30));

            return result;
        }
    }
}
